package com.tan.lbs.service.impl;

import java.util.ArrayList;

import com.tan.lbs.dao.DialogNodeDAO;
import com.tan.lbs.dao.LocationDAO;
import com.tan.lbs.dao.MediaDAO;
import com.tan.lbs.dao.NPCDAO;
import com.tan.lbs.dao.impl.DialogNodeDAOImpl;
import com.tan.lbs.dao.impl.LocationDAOImpl;
import com.tan.lbs.dao.impl.MediaDAOImpl;
import com.tan.lbs.dao.impl.NPCDAOImpl;
import com.tan.lbs.entity.DialogNode;
import com.tan.lbs.entity.Location;
import com.tan.lbs.entity.NPC;
import com.tan.lbs.entity.NPCHeader;
import com.tan.lbs.service.NPCService;

public class NPCServiceImpl implements NPCService {
	
	private NPCDAO npcDAO;
	private LocationDAO locationDAO;
	private MediaDAO mediaDAO;
	private DialogNodeDAO dialogNodeDAO;
	
	public NPCServiceImpl() {
		npcDAO = new NPCDAOImpl();
		locationDAO = new LocationDAOImpl();
		mediaDAO = new MediaDAOImpl();
		dialogNodeDAO = new DialogNodeDAOImpl();
	}

	@Override
	public String createNPCWithoutLocation(NPC npc) {
		String npcID = npcDAO.create(npc);
		//This is the start dialog node for the NPC;
		DialogNode dialogNode = new DialogNode();
		dialogNode.setName("Start dialog");
		dialogNode.setInteractionType("display");
		dialogNode.setNodeType("progress");
		String dialogID = dialogNodeDAO.create(dialogNode);
		npc.setDialogID(dialogID);
		npcDAO.update(npc);
		return npcID;
	}

	@Override
	public void modifyNPCWithoutLocation(NPC npc) {
		NPC oldNPC  = npcDAO.readByID(npc.getNpcID());
		npc.setLocationID(oldNPC.getLocationID());
		npc.setIconID(oldNPC.getIconID());
		npc.setFigureID(oldNPC.getFigureID());
		npc.setDialogID(oldNPC.getDialogID());
		npcDAO.update(npc);		
	}

	@Override
	public NPC readNPCDetails(String npcID) {
		return npcDAO.readByID(npcID);
	}

	@Override
	public String createLocation(Location location, String npcID) {
		NPC npc = npcDAO.readByID(npcID);
		String locationID = locationDAO.create(location);
		npc.setLocationID(locationID);
		npcDAO.update(npc);
		return locationID;
	}

	@Override
	public ArrayList<NPCHeader> retrieveNPCList(String gameID) {
		ArrayList<NPC> npcs = npcDAO.searchByGame(gameID);
		ArrayList<NPCHeader> headers = new ArrayList<NPCHeader>();
		for(NPC npc : npcs) {
			NPCHeader header = new NPCHeader();
			if(npc.getLocationID()!=null) {
				header.setLocation(locationDAO.readByID(npc.getLocationID()));
			}
			header.setNpcID(npc.getNpcID());
			header.setNpcName(npc.getName());
			headers.add(header);
		}
		return headers;
	}

	@Override
	public void deleteLocation(String npcID, String locationID) {
		NPC npc = npcDAO.readByID(npcID);
		npc.setLocationID(null);
		npcDAO.update(npc);
		locationDAO.delete(locationID);

	}

	@Override
	public void deleteNPCAndLocation(String npcID) {
		String locationID = npcDAO.readByID(npcID).getLocationID();
		String iconID = npcDAO.readByID(npcID).getIconID();
		String mediaID = npcDAO.readByID(npcID).getFigureID();
		npcDAO.delete(npcID);
		locationDAO.delete(locationID);
		mediaDAO.delete(iconID);
		mediaDAO.delete(mediaID);
	}

}
